<template>
  <div class="user-container">
    <!-- 顶部导航栏 -->
    <van-nav-bar title="用户中心" class="top-nav" />
    <!-- 用户账号信息展示区域 -->
    <div class="user-account-info-box" @click="goUserDetailInfo">
      <!-- 用户头像 -->
      <div class="user-head-picture-box">
        <img :src="userHeadPicture" alt="">
      </div>
      <!-- 用户名 -->
      <div class="username">
        {{username}}
      </div>
      <!-- 用户账号 -->
      <div class="user-account">
        用户账号: {{userAccount}}
      </div>
      <!-- 向右箭头图标 -->
      <van-icon name="arrow" class="right-arrow"/>
    </div>
    <!-- 我的订单(待支付 待收货 待评价) -->
    <div class="my-order-box" :border="false">
      <div class="font-box" @click="$router.push({name: 'allOrder', query: {active: 0}})">
        <span>我的订单</span>
        <span>全部<van-icon name="arrow" /></span>
      </div>
      <div class="icon-box">
        <div @click="$router.push({name: 'allOrder', query: {active: 1}})">
          <van-icon name="balance-pay icon" :dot="orderWaitPayCount>0 ? true : false" />
          <div>待支付</div>
        </div>
        <div @click="$router.push({name: 'allOrder', query: {active: 2}})">
          <van-icon name="logistics icon" :dot="orderWaitReceiveCount>0 ? true : false" />
          <div>待收货</div>
        </div>
        <!-- <div>
          <van-icon name="chat-o icon" />
          <div>待评价</div>
        </div> -->
      </div>
    </div>
    <!-- 我的收货地址 -->
    <!-- 我的评价 -->
    <div class="box">
      <div class="address-box" @click="goUserReceiveAddress">
        <span><van-icon name="records icon" />我的收货地址</span>
        <span><van-icon name="arrow"/></span>
      </div>
      <!-- <div class="comment-box">
        <span><van-icon name="smile-comment-o icon" />我的评价</span>
        <span><van-icon name="arrow"/></span>
      </div> -->
    </div>
    <!-- 设置(修改密码与退出登录) -->
    <div class="box" @click="goSetting">
      <div>
        <span><van-icon name="setting-o icon" />设置</span>
        <span><van-icon name="arrow"/></span>
      </div>
    </div>
  </div>
</template>

<script>
import { Grid, GridItem } from 'vant';

export default {
  name: 'User',
  components: {
    [Grid.name]: Grid,
    [GridItem.name]: GridItem,
  },
  data() {
    return {
      username: '',
      userAccount: '',
      userHeadPicture: '',
      orderWaitPayCount: 0,
      orderWaitReceiveCount: 0,
    }
  },
  methods: {
    // 进入用户详细信息页面
    goUserDetailInfo() {
      this.$router.push({name: 'userdetailinfo'})
    },
    // 进入用户收货地址页面
    goUserReceiveAddress() {
      this.$router.push({name: 'receiveAddress'})
    },
    // 进入设置界面
    goSetting() {
      this.$router.push({name: 'setting'})
    },
  },
  created() {
    // 获取用户账号信息
    this.req.get(
      'user/username/userAccount/',
      {
        params: {
          token: localStorage.getItem('token')
        }
      }
    ).then(res=>{
      this.username = res.data.data.username
      this.userAccount = res.data.data.userAccount
      this.userHeadPicture = res.data.data.userHeadPicture
      if (this.userHeadPicture.length <= 0) {
        this.userHeadPicture = this.userConstants.USER_HEAD_PICTURE
      }
    }, err=>{
      console.log(err)
    })
    // 获取待支付订单个数和待收货订单个数
    this.req.post(
      'user/get/order/number/',
      {
        token: localStorage.getItem('token')
      }
    ).then(res=>{
      if (res.data.mark === 1) {
        this.orderWaitPayCount = res.data.data.orderWaitPayCount
        this.orderWaitReceiveCount = res.data.data.orderWaitReceiveCount
      }
    }, err=>{})
  }
}
</script>

<style lang="less" scoped>
// 用户中心界面容器的样式
.user-container {
  height: 93vh;
  background-color: #eee;
}
// 顶部导航栏样式
.top-nav {
  border-bottom: 1px solid #eee;
}
// 用户账号信息展示区域样式
.user-account-info-box {
  position: relative;
  box-sizing: border-box;
  width: 100%;
  height: 7rem;
  margin: 0;
  padding: 1rem 1rem;
  background-color: #fff;
  // 头像样式
  .user-head-picture-box {
    overflow: hidden;
    height: 5rem;
    width: 5rem;
    border-radius: 1.5rem;
    img {
      width: 100%;
      // height: 100%;
    }
  }
  // 用户名
  .username {
    position: absolute;
    top: 1.5rem;
    left: 8rem;
    font-size: 1.5rem;
  }
  // 用户账号
  .user-account {
    position: absolute;
    top: 4rem;
    left: 8rem;
    color: #888;
  }
  // 向右箭头样式
  .right-arrow {
    position: absolute;
    top: 2.5rem;
    right: 1rem;
    font-size: 2rem;
    color: #ddd;
  }
}
// 我的订单样式
.my-order-box {
  margin: 0.5rem auto 0;
  width: 98%;
  border-radius: 1rem;
  background-color: #fff;
  // “我的订单”文字样式
  .font-box {
    padding: 0.5rem;
    span:nth-child(1) {
      float: left;
      font-weight: 700;
    }
    span:nth-child(2) {
      float: right;
      font-size: 0.8rem;
      color: #999;
    }
  }
  // 清除浮动
  .font-box::after{ 
    /* after 伪元素必须有content属性 */ 
    content: ""; 
    /* 默认是行内元素，需要转化为块元素 */ 
    display: block; 
    height: 0; 
    /* 清除左右浮动的影响 */ 
    clear: both; 
    visibility: hidden;
  }
  // 图标盒子样式
  .icon-box {
    display: flex;
    padding: 0 2rem;
    // justify-content: space-between;
    justify-content: space-around;
    text-align: center;
    // 图标样式
    .icon {
      margin: 1rem 1.5rem 0.5rem;
      font-size: 2rem;
    }
    // 图标对应文字样式
    div {
      padding-bottom: 0.5rem;
      font-size: 0.9rem;
    }
  }
}
// 列表样式
.box {
  margin: 0.5rem 0.5rem 0;
  padding: 0.5rem 0.5rem;
  border-radius: 1rem;
  background-color: #fff;
  div {
    height: 3rem;
    line-height: 3rem;
    span:nth-child(1) {
      float: left;
      .icon {
        margin: 0 1rem 0 0.5rem;
        vertical-align: middle;
        font-size: 1.8rem;
      }
    }
    span:nth-child(2) {
      float: right;
    }
  }
  // 清除浮动
  div::after {
    /* after 伪元素必须有content属性 */ 
    content: ""; 
    /* 默认是行内元素，需要转化为块元素 */ 
    display: block; 
    height: 0; 
    /* 清除左右浮动的影响 */ 
    clear: both; 
    visibility: hidden;
  }
}
</style>